/*
source of inspiration:
http://www.chadvernon.com/blog/tutorials/directx9/terrain-generation-with-a-heightmap/
*/


#ifndef _HEIGHTMAP_H
#define _HEIGHTMAP_H

#include "siut\simd\Vec3f.hpp"

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <vector>
#include <iostream>


using siut::simd::Vec3f;
using std::vector;


class Heightmap
{
	public:
	Heightmap();
	~Heightmap();
	//void loadFromFile(string filename);
	void loadHeightmap(const std::string& filename);
	void loadNormalmap(const std::string& filename);
	GLuint createVBO(const void* data, int dataSize, GLenum target, GLenum usage);
	void createVertexBufferData();
	void render();
	void renderPickingColors();
	void setPickPoint(int mousx, int mousy);
	Vec3f getPickedPoint();
	//void setTexture(GLuint texhandle);

	public:
	vector< vector<float> > heights;
	unsigned long heightmapWidth, heightmapLength;
	float heightScale;
	GLuint texHeightmap;

private:
	void initFBO();
	void releaseFBO();
	void calcPickedPoint();
	

	GLuint heightmapVertColorsVA;
	GLuint heightmapTexCoordsVA;
	GLuint heightmapNormalsVA;
	GLuint heightmapVerticesVA;
	GLuint heightmapIndicesVA;

	vector<GLfloat> vertColors;
	vector<GLfloat> texCoords;
	vector<GLfloat> normals;
	vector<GLfloat> vertices;
	vector<GLuint> indices;	

	GLuint fb; // framebuffer obj handle.
	GLuint color_rb; // color renderbuffer attached to framebuffer
	int fboWidth;
	int fboHeight;

	void enableRenderBuffer();
	void disableRenderBuffer();
	int oldScreenWidth;
	int oldScreenHeight;
	Vec3f pickedPoint;
	bool isPickUpdated;
	int mPickX;
	int mPickY;


};

#endif